#include "offlinemsgmodel.hpp"
#include "mysql.hpp"

using namespace std;

OfflineMsgModel::OfflineMsgModel() {}
OfflineMsgModel::~OfflineMsgModel() {}

void OfflineMsgModel::Insert(const int id, const string& msg) {
    char sql[1024]{0};
    sprintf(sql, "INSERT INTO offlinemessage (userid, message) VALUES (%d, '%s')", id, msg.c_str());
    MySql mysql;
    bool ret = mysql.connect();
    if (ret) {
        mysql.query(sql);
    }
    
}
vector<string> OfflineMsgModel::Query(const int id) {
    vector<string> result;
    char sql[1024]{0};
    sprintf(sql, "SELECT message FROM offlinemessage WHERE userid = %d", id);
    MySql mysql;
    bool ret = mysql.connect();
    if (ret) {
        MYSQL_RES* res = mysql.query(sql);
        if (res) {
            MYSQL_ROW row;
            while ((row = mysql_fetch_row(res))) {
                result.push_back(row[0]);
            }
            mysql_free_result(res);
        }
    }
    return result;
}

void OfflineMsgModel::Delete(const int id) {
    char sql[1024]{0};
    sprintf(sql, "DELETE FROM offlinemessage WHERE userid = %d", id);
    MySql mysql;
    bool ret = mysql.connect();
    if (ret) {
        mysql.query(sql);
    }
}